import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'

Vue.use(Router)

// 开发环境不使用懒加载, 因为懒加载页面太多的话会造成webpack热更新太慢, 所以只有生产环境使用懒加载
const _import = require('./import-' + process.env.NODE_ENV)

// 全局路由(无需嵌套上tab整体布局)
const globalRoutes = [
  //{ path: '/404', component: _import('common/404'), name: '404', meta: { title: '404未找到' } },
  { path: '/test', component: _import('modules/mutual/test'), name: 'test', meta: { title: '测试页面' } },
  { path: '/register', component: _import('modules/mutual/register'), name: 'register', meta: { title: '注册' } },
  { path: '/login', component: _import('modules/mutual/login'), name: 'login', meta: { title: '登录' } },
  { path: '/findPassword', component: _import('modules/mutual/findPassword'), name: 'findPassword', meta: { title: '找回密码' } },
  { path: '/protocol', component: _import('modules/mutual/protocol'), name: 'protocol', meta: { title: '找回密码' } },
  { path: '/member', component: _import('modules/mutual/member'), name: 'member', meta: { title: '会员中心' } },
  { path: '/certification', component: _import('modules/mutual/certification'), name: 'certification', meta: { title: '认证中心' } },
  { path: '/payment', component: _import('modules/mutual/payment'), name: 'payment', meta: { title: '转账' } },
  { path: '/paymentLog', component: _import('modules/mutual/paymentLog'), name: 'paymentLog', meta: { title: '转账日志' } },
  { path: '/setup', component: _import('modules/mutual/setup'), name: 'setup', meta: { title: '账户设置' } },
  { path: '/scan', component: _import('modules/mutual/scan'), name: 'scan', meta: { title: '扫一扫' } },
  { path: '/personalData', component: _import('modules/mutual/personalData'), name: 'personalData', meta: { title: '个人资料' } },
  { path: '/drawings', component: _import('modules/mutual/drawings'), name: 'drawings', meta: { title: '商家笑笑宝提款' } },
  { path: '/accounts', component: _import('modules/mutual/accounts'), name: 'accounts', meta: { title: '银行卡账户管理' } },
  { path: '/addAccounts', component: _import('modules/mutual/addAccounts'), name: 'addAccounts', meta: { title: '添加银行卡' } },
  { path: '/purchase', component: _import('modules/mutual/purchase'), name: 'purchase', meta: { title: '买入购物劵' } },
  { path: '/xiaoxiao', component: _import('modules/mutual/xiaoxiao'), name: 'xiaoxiao', meta: { title: '笑笑宝账号展示' } },
]

// 主入口路由(需嵌套上tab整体布局)
const mainRoutes = {
  path: '/',
  component: _import('main'),
  name: 'main',
  redirect: { name: 'home' },
  meta: { title: '主入口整体布局' },
  children: [
    { path: '/home', component: _import('modules/menus/home'), name: 'home', meta: { title: '首页' } },
    { path: '/catalog', component: _import('modules/menus/catalog'), name: 'catalog', meta: { title: '分类' } },
    { path: '/cart', component: _import('modules/menus/cart'), name: 'cart', meta: { title: '购物车' } },
    { path: '/personal', component: _import('modules/menus/personal'), name: 'personal', meta: { title: '个人中心' } },
  ]
}

const router = new Router({
  mode: 'hash',
  scrollBehavior: () => ({ y: 0 }),
  isAddDynamicMenuRoutes: false,// 是否已经添加动态(菜单)路由
  routes: globalRoutes.concat(mainRoutes)
})

router.beforeEach((to, from, next) => {
  store.commit('common/updateCurrentPath',to.path);
  next();
})

export default router
